package com.pengtu.gsj.service.impl.check.lzgl.overloadMan;

import java.util.ArrayList;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.alibaba.fastjson.JSONObject;
import com.pengtu.gsj.dao.base.QueryFilter;
import com.pengtu.gsj.dao.lzgl.GovernSpeedDao;
import com.pengtu.gsj.entity.app.Attachment;
import com.pengtu.gsj.entity.check.CheckTable;
import com.pengtu.gsj.entity.lzgl.GovernSpeed;
import com.pengtu.gsj.service.check.CheckTableService;
import com.pengtu.gsj.service.check.lzgl.overloadMan.FlowOverloadEnforService;
import com.pengtu.gsj.service.system.SystemManageService;
import com.pengtu.gsj.utils.MathsUtils;


@Service
@Transactional
public class FlowOverloadEnforServiceImpl implements FlowOverloadEnforService {
	@Autowired
	private CheckTableService checkTableService;
	
	@Autowired
	private SystemManageService systemManageService;
	
	@Autowired
	private GovernSpeedDao governSpeedDao;
	
	@Override
	public List<GovernSpeed> getGovernSpeedList(QueryFilter queryFilter){
		return governSpeedDao.find(queryFilter, "testYear", true);
	}
	
	
	@Override
	public Map<String, Object> getDevelopPlanmMap(String startTime, String endTime, CheckTable checkTable, String tag)throws Exception{
		Map<String, Object> map = new HashMap<String, Object>();
		List<GovernSpeed> lawList = new ArrayList<GovernSpeed>();
		
		JSONObject jsonObject = JSONObject.parseObject(checkTable.getRetrieval());
		double yearFull = Double.parseDouble((String)jsonObject.get("yearFull"));
		double yearPart = Double.parseDouble((String)jsonObject.get("yearPart"));
		String governSpeedId = (String)jsonObject.get("governSpeedId");
		
		//取到所有文件
		List<Attachment> list = systemManageService.getAllCheckAttachments(startTime, endTime, tag);
		//获取规章制度文件
		List<Attachment> zdList = systemManageService.getAllCheckAttachments("ldzczf_gzzd");
		//获取执法记录文件
		List<Attachment> jlList = systemManageService.getAllCheckAttachments("ldzczf_zfjl");
		
		double total =0; 								//检查得分
		double percentage =0; 							//系统完善度
		double tableFull  = yearFull + yearPart*4.0;	//满分
		
		if (StringUtils.isNotEmpty(governSpeedId)) {
			//是否显示表格二的开关
			map.put("flag", true);
			String[] idArrs = governSpeedId.split(",");
			for (int i = 0; i < idArrs.length; i++) {
				if (i > 3) {
					break;
				}
				GovernSpeed governSpeed = governSpeedDao.get(idArrs[i]);
				lawList.add(governSpeed);
			}
		}
		total += zdList.size() > 0 ? yearFull : 0;
		total += lawList.size() > 0 ? lawList.size()*yearPart : 0;
		
		//判断完善度
		percentage = MathsUtils.divDouble(total *100 , tableFull, 2) ;
		checkTableService.updateCheckTable(checkTable, percentage, total, tableFull, true);
		map.put("percent", percentage);
		map.put("att", list);
		map.put("lawList", lawList);
		map.put("zdList", zdList);
		map.put("jlList", jlList);
		map.put("scoreOne", yearFull);
		map.put("scoreTwo", lawList.size()*yearPart);
		map.put("score", checkTable.getTestFull());
		map.put("checkTable", checkTable);
		
		return map;
	}
}
